package com.orphan.marketFront.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.orphan.marketFront.mapper.UserInfoMapper;
import com.orphan.marketFront.po.AllDeposit;
import com.orphan.marketFront.po.UserInfo;
import com.orphan.marketFront.service.IAllDepositService;
import com.orphan.marketFront.service.IDepositService;
import com.orphan.marketFront.service.IUserService;
import com.orphan.marketFront.util.StringUtil;
import com.orphan.util.dto.DepositDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lucangtian
 * @since 2021-07-19
 */
@Api(tags = "预存款接口")
@RestController
@RequestMapping("/deposit")
public class DepositController {

    @Autowired
    IDepositService depositService;
    @Autowired
    IAllDepositService allDepositService;
    @Autowired
    IUserService userService;
    @Autowired
    UserInfoMapper userInfoMapper;

    @ApiOperation(value = "预存款方法", notes = "预存款方法")
    @PostMapping("saveMoney")
    public Map<String, Object> saveMoney(Long id,int money){
        Map<String, Object> map = new HashMap<String, Object>();

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String time = df.format(new Date());

        int deposit = depositService.deposit(id, money, time);

        //本次存款传输对象
        String userName = userService.getUserNameById(id);
        DepositDto depositDto=new DepositDto();
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name", userName);
        UserInfo userInfo = userInfoMapper.selectOne(queryWrapper);
        System.out.println("userInfo = " + userInfo);
        Long uId = userInfo.getUId();
        depositDto.setUId(uId);
        depositDto.setUserName(userName);
        depositDto.setMoney(money);
        depositDto.setTime(time);
        System.out.println("depositDto = " + depositDto);
        //用mq发送到审核系统
        //todo
        depositService.sendDepositInfo(depositDto);

        if (deposit > 0) {
            map.put("msg", StringUtil.DEPOSIT_SUCCESS);
            return map;
        }
        map.put("msg", StringUtil.DEPOSIT_FAILURE);
        return map;
    }

    @PostMapping("getDeposit")
    public Map<String, Object> getDeposit(String userName){
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name", userName);
        UserInfo userInfo = userInfoMapper.selectOne(queryWrapper);
        Long uId = userInfo.getUId();

        AllDeposit allDeposit = allDepositService.selectById(uId);
        System.out.println(allDeposit);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("allDeposit", allDeposit.getAccumulatedDeposit().toString());
        map.put("uId", allDeposit.getUId().toString());
        return map;
    }
}
